<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="105px">
      <el-form-item label="领料单编号" prop="workorderCode">
        <el-input
          v-model="queryParams.issueCode"
          placeholder="请输入领料单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品物料编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="领料时间范围">
        <el-date-picker
          v-model="value1"
          type="daterange"
          range-separator="至"
          start-placeholder="领料开始时间"
          end-placeholder="领料结束时间"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="领料单编号" align="center" prop="issueCode" />
      <el-table-column label="领料时间" align="center" prop="issueDate" width="180">
        <template slot-scope="scope">{{ parseTime(scope.row.issueDate, '{y}-{m}-{d}') }}</template>
      </el-table-column>
      <el-table-column label="领料人" align="center" prop="issueNickName" />
      <el-table-column label="产品物料编码" align="center" prop="itemCode" />
      <el-table-column label="产品物料名称" align="center" prop="itemName" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="领料数量" align="center" prop="quantityIssued" />
      <el-table-column label="期初" align="center" prop="quantityBegan" />
      <el-table-column label="期末剩余数量（最新）" align="center" prop="quantityEnd" />
      <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    
  </div>
</template>

<script>
import {listItemIssueConsume} from "@/api/mes/pro/productionMaterials";
export default {
  name: 'productionMaterials',
  data() {
    return {
      loading: false,
      total: 0,
      queryParams:{
        pageNum: 1,
        pageSize: 10,
        issueCode: null,
        itemName: null,
        itemCode: null,
        issueStartDate: null,
        issueEndDate: null,
      },
      value1: this.getDefaultDateRange(),
      list: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleQuery() {
      this.getList()
      console.log(this.queryParams, this.value1);
    },
    resetQuery() {
      this.queryParams = {
        issueCode: null,
        itemName: null,
        itemCode: null,
        issueStartDate: null,
        issueEndDate: null,
      }
      this.value1 = this.getDefaultDateRange()
      this.handleQuery()
    },
    getList() {
      this.loading = true
      this.queryParams.issueStartDate = this.value1[0]
      this.queryParams.issueEndDate = this.value1[1]
      listItemIssueConsume(this.queryParams).then(res => {
        this.list = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    handleDetail(row) {
      console.log(row)
    },
    getDefaultDateRange() {
      const today = new Date();
      const thirtyDaysAgo = new Date();
      thirtyDaysAgo.setDate(today.getDate() - 30);
      const formatDate = (date) => {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
      };
      return [formatDate(thirtyDaysAgo), formatDate(today)];
    },
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  
}
</style>